const express = require('express');
const router = express.Router();
const pool = require('../config/db');
const { formatResponse } = require('../utils/response');


// 获取单个商品
router.get('/:id', async (req, res) => {
  try {
    const productId = req.params.id;
    const [rows] = await pool.execute('SELECT * FROM products WHERE id = ?', [productId]);
    
    if (rows.length === 0) {
      return res.status(404).json(formatResponse(404, '商品不存在'));
    }
    const product = rows[0];
    res.json(formatResponse(200, '商品详情获取成功', product));
  } catch (error) {
    res.status(500).json(formatResponse(500, '获取商品详情失败', error.message));
  }
});

module.exports = router;    